package cn.daniellee.plugin.awa;

import cn.daniellee.plugin.awa.command.MissionCommand;
import cn.daniellee.plugin.awa.command.MissionCompleter;
import cn.daniellee.plugin.awa.runnable.HttpRunnable;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.scheduler.BukkitTask;

public class AuthWebApi extends JavaPlugin {

    private static AuthWebApi instance;

    private String prefix;

    private BukkitTask httpTask;

    private String aesKey;

    @Override
    public void onEnable() {
        instance = this;

        if (loadConfig()) {
            getLogger().info(" ");
            getLogger().info(">>>>>>>>>>>>>>>>>>>>>>>> AuthWebApi Loaded <<<<<<<<<<<<<<<<<<<<<<<<");
            getLogger().info(">>>>> If you encounter any bugs, please contact author's QQ: 768318841 <<<<<");
            getLogger().info(" ");

            Bukkit.getPluginCommand("authwebapi").setExecutor(new MissionCommand());
            Bukkit.getPluginCommand("authwebapi").setTabCompleter(new MissionCompleter());

            httpTask = new HttpRunnable().runTaskAsynchronously(this);
        }
    }

    public boolean loadConfig() {
        getLogger().info("[AuthWebApi] Loading config...");
        if(!getDataFolder().exists()) getDataFolder().mkdirs();
        prefix = "&7[&6" + getConfig().getString("prompt-prefix", "AuthWebApi") + "&7] &3: &r";
        aesKey = getConfig().getString("setting.aes-key", "Example");
        saveDefaultConfig();
        return true;
    }

    @Override
    public void onDisable() {
        if (httpTask != null) httpTask.cancel();

        getLogger().info(" ");
        getLogger().info(">>>>>>>>>>>>>>>>>>>>>>>> AuthWebApi Unloaded <<<<<<<<<<<<<<<<<<<<<<<<");
        getLogger().info(" ");
    }

    public static AuthWebApi getInstance() {
        return instance;
    }

    public String getPrefix() {
        return prefix;
    }

    public String getAesKey() {
        return aesKey;
    }
}
